package com.exteam.model.manager;

import com.exteam.common.network.HttpIntent;
import com.exteam.common.network.IRequest;
import com.exteam.common.network.RequestManager;
import com.exteam.common.util.CommLogger;
import com.exteam.common.util.CommUtils;
import com.exteam.model.URLContainer;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 功能描述：网络接口，从服务器获取数据
 *
 * @author yangz
 */
public class NetManager {

    private static final String APP_KEY = "FPX_2015_KEY";

    /**
     * 公共接口，设置头信息
     *
     * @return
     */
    public static HashMap<String, String> getHeader() {
        HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("appkey", "B8A4986E1B9CDDD6");
        headerMap.put("apikey", "exteam");
        headerMap.put("Content-Type", "application/json");
        headerMap.put("key", "11766cb8e6054ddd0699b47c787b5fa9");

        return headerMap;
    }

    /**
     * 公共接口，所有接口的包装
     *
     * @return
     */
    public static JSONObject getJSONObject(JSONObject object) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("values", object);
        } catch (JSONException es) {
            CommLogger.d("netmanager getJSONObject " + es.toString());
        }
        return obj;
    }

    /**
     * 1.获取验证码
     *
     * @param mobileNumber 手机号
     * @param clientType   客户端类型
     * @param callBack
     */
    public static void getValidateCode(String mobileNumber, int clientType, IRequest.IRequestCallBack callBack) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("mobileNumber", mobileNumber);
            obj.put("clientType", clientType);
            obj.put("valueKey", CommUtils.md5(mobileNumber + clientType + APP_KEY));
        } catch (JSONException e) {
            CommLogger.d("NetManager=========getValidateCode====异常");
        }
        String url = URLContainer.getRequestUrl(URLContainer.GET_VALIDATE_CODE);
        HttpIntent httpIntent = new HttpIntent(url, getJSONObject(obj));
        httpIntent.headers = getHeader();
        new RequestManager().requestByPostJSON(httpIntent, callBack);
    }

    /**
     * 2.登录
     *
     * @param mobileNumber 手机号
     * @param validateCode 验证码
     * @param callBack
     */
    public static void requestLogin(String mobileNumber, String validateCode, IRequest.IRequestCallBack callBack) {
        JSONObject obj = new JSONObject();
        try {
            obj.put("mobileNumber", mobileNumber);
            obj.put("validateCode", validateCode);
            obj.put("valueKey", CommUtils.md5(mobileNumber + validateCode + APP_KEY));
        } catch (JSONException e) {
            CommLogger.d("NetManager=========requestLogin====异常");
        }
        String url = URLContainer.getRequestUrl(URLContainer.LOGIN);
        HttpIntent httpIntent = new HttpIntent(url, getJSONObject(obj));
        httpIntent.headers = getHeader();
        new RequestManager().requestByPostJSON(httpIntent, callBack);
    }

//    /**
//     * 上传文件
//     *
//     * @param uploaderId   上传者ID
//     * @param uploaderType 上传者类型
//     * @param uploadFrom   上传来源
//     * @param file         文件
//     * @param callBack
//     */
//    public static void uploadFile(int uploaderId, int uploaderType, int uploadFrom, File file, final RequestCallBack callBack) {
//        HttpUtils http = new HttpUtils();
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("uploaderId", uploaderId + "");
//        params.addBodyParameter("uploaderType", uploaderType + "");
//        params.addBodyParameter("uploadFrom", uploadFrom + "");
//        params.addBodyParameter("file", file);
//        params.addBodyParameter("fileName", file.getName());
//        http.send(HttpRequest.HttpMethod.POST, URLContainer.getRequestUrl(URLContainer.EVENT_UPLOAD_FILE), params, callBack);
//    }


}
